package com.haina.shop.service.impl;


import com.github.pagehelper.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.haina.shop.domain.ProdComm;
import com.haina.shop.domain.User;
import com.haina.shop.dto.CommPageDTO;
import com.haina.shop.mapper.UserMapper;
import com.haina.shop.result.IPageIndexImg;
import com.haina.shop.result.ServiceResult;
import com.haina.shop.service.ProdCommService;
import com.haina.shop.mapper.ProdCommMapper;
import com.haina.shop.vo.CommPageVo;
import com.haina.shop.vo.ProdCommVo;
import com.haina.shop.vo.ProdCommVo1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 48077
* @description 针对表【tz_prod_comm(商品评论)】的数据库操作Service实现
* @createDate 2024-07-31 18:34:29
*/
@Service
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm>
    implements ProdCommService{
    @Autowired
    private ProdCommMapper prodCommMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public ProdCommVo getNumber(Long prodId) {
        ProdCommVo prodCommVo = new ProdCommVo();
        int number = prodCommMapper.selectCountByProdId(prodId);
        int good = prodCommMapper.selectGoodNumberById(prodId);
        int min = prodCommMapper.selectMinNumberById(prodId);
        int bad = prodCommMapper.selectBadNumberById(prodId);
        int price = prodCommMapper.priceNumber(prodId);
        double positiveRating =(double) good/number;
        prodCommVo.setNumber(number);
        prodCommVo.setPicNumber(price);
        prodCommVo.setPraiseNumber(good);
        prodCommVo.setSecondaryNumber(min);
        prodCommVo.setNegativeNumber(bad);
        prodCommVo.setPositiveRating(positiveRating);
        return prodCommVo;

    }


    @Override
    public ServiceResult<IPageIndexImg<ProdCommVo1>> getPage1(CommPageDTO commPageDTO) {
        //获取当前页码
        Long currentPage = commPageDTO.getCurrent();
        //获取每页显示的条数
        Long size= commPageDTO.getSize();
        //设置分页初始值
        PageHelper.startPage(Math.toIntExact(currentPage), Math.toIntExact(size));

        Page<ProdComm> page = prodCommMapper.getPage(commPageDTO);

        IPageIndexImg<ProdCommVo1>  iPageIndexImg = new IPageIndexImg<>();
        iPageIndexImg.setTotal(page.getTotal());
        iPageIndexImg.setCurrent(currentPage);
        iPageIndexImg.setSize(size);
        iPageIndexImg.setPages(page.getTotal()/size+1);
        List<ProdComm> result = page.getResult();
        List<ProdCommVo1> data = new ArrayList<>();
        for (ProdComm prodComm : result) {
           ProdCommVo1 prodComm1 = new ProdCommVo1();
           prodComm1.setProdCommId(prodComm.getProdCommId());
           prodComm1.setScore(prodComm.getScore());
           prodComm1.setIsAnonymous(prodComm.getIsAnonymous());
           prodComm1.setReplyContent(prodComm.getReplyContent());
           prodComm1.setRecTime(prodComm.getRecTime());
           prodComm1.setReplyTime(prodComm.getReplyTime());
           prodComm1.setUserId(prodComm.getUserId());

           User user = userMapper.selectById(prodComm.getUserId());

           prodComm1.setNickName(user.getNickName());
           prodComm1.setPic(user.getPic());
           prodComm1.setReplySts(prodComm.getReplySts());
           prodComm1.setPics(prodComm.getPics());
           prodComm1.setEvaluate(prodComm.getEvaluate());
           prodComm1.setContent(prodComm.getContent());
           data.add(prodComm1);

        }
        iPageIndexImg.setRecords(data);
        return ServiceResult.success(iPageIndexImg);

    }
}




